const DB = require('../tools/mysql')

module.exports = async ctx => {

  const { state, cityid, roleid, userid } = ctx.request.body

  if (state == 0) {
    const data = await DB('issue_list as a').select(
      'a.id',
      'a.prefecture',
      'b.name as cityname',
      'a.town',
      'c.name as townname',
      'a.village',
      'd.name as villagename',
      'a.inspector',
      'e.name as inspectorname',
      'a.time',
      'a.submittime as btime',
      'a.state'
    )
    .leftJoin('placename as b', 'b.id', 'a.prefecture')
    .leftJoin('placename as c', 'c.id', 'a.town')
    .leftJoin('placename as d', 'd.id', 'a.village')
    .leftJoin('user as e', 'e.id', 'a.inspector')
    .where('a.inspector', userid)
    .andWhere('a.enable', 1)
    .andWhere('a.state', 3)

    ctx.body = { code: 1, msg: '查询成功', data }
  } else if (state == 1) {
    const data = await DB('issue_list as a').select(
      'a.id',
      'a.prefecture',
      'b.name as cityname',
      'a.town',
      'c.name as townname',
      'a.village',
      'd.name as villagename',
      'a.inspector',
      'e.name as inspectorname',
      'a.time',
      'a.transfertime1 as btime',
      'a.state',
      'e.place'
    )
    .leftJoin('placename as b', 'b.id', 'a.prefecture')
    .leftJoin('placename as c', 'c.id', 'a.town')
    .leftJoin('placename as d', 'd.id', 'a.village')
    .leftJoin('user as e', 'e.id', 'a.inspector')
    .andWhere('a.enable', 1)
    .andWhere('a.state', 4)

    let list = []
    data.forEach(item => {
      const place = item.place.split(',')
      for (let i in place) {
        if (cityid.split(',').indexOf(place[i] + '') > -1) {
          list.push(item)
          break
        }
      }
    })

    ctx.body = { code: 1, msg: '查询成功', data: list }
  } else if (state == 2) {
    const data = await DB('issue_list as a').select(
      'a.id',
      'a.prefecture',
      'b.name as cityname',
      'a.town',
      'c.name as townname',
      'a.village',
      'd.name as villagename',
      'a.inspector',
      'e.name as inspectorname',
      'a.time',
      'a.transfertime2 as btime',
      'a.state',
      'e.place'
    )
    .leftJoin('placename as b', 'b.id', 'a.prefecture')
    .leftJoin('placename as c', 'c.id', 'a.town')
    .leftJoin('placename as d', 'd.id', 'a.village')
    .leftJoin('user as e', 'e.id', 'a.inspector')
    .andWhere('a.enable', 1)
    .andWhere('a.state', 5)

// 后面去做处理（只看处理员负责的镇、村）
    let list = []
    data.forEach(item => {
      const place = item.place.split(',')
      for (let i in place) {
        if (cityid.split(',').indexOf(place[i] + '') > -1) {
          list.push(item)
          break
        }
      }
    })
    ctx.body = { code: 1, msg: '查询成功', data: list }
  } else if (state == 3) {
    const data = await DB('issue_list as a').select(
      'a.id',
      'a.prefecture',
      'b.name as cityname',
      'a.town',
      'c.name as townname',
      'a.village',
      'd.name as villagename',
      'a.inspector',
      'e.name as inspectorname',
      'a.time',
      'a.overtime as btime',
      'a.state',
      'e.place'
    )
    .leftJoin('placename as b', 'b.id', 'a.prefecture')
    .leftJoin('placename as c', 'c.id', 'a.town')
    .leftJoin('placename as d', 'd.id', 'a.village')
    .leftJoin('user as e', 'e.id', 'a.inspector')
    .andWhere('a.enable', 1)
    .andWhere('a.state', 6)

// 后面去做处理（只看处理员负责的镇、村）
    let list = []
    data.forEach(item => {
      const place = item.place.split(',')
      for (let i in place) {
        if (cityid.split(',').indexOf(place[i] + '') > -1) {
          list.push(item)
          break
        }
      }
    })
    ctx.body = { code: 1, msg: '查询成功', data: list }
  }

}